﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Metadata;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace WpfApp2
{
    public class MainWindowViewModel: ViewModelBase
    {
        #region 动态属性集合
        private List<Students> _students;
        public List<Students> Students
        {
            get => _students;
            set => SetProperty(ref _students, value); // 使用 SetProperty 方法简化逻辑
        }


        private Students _selectedStudent;
       
        public Students SelectedStudent
        {
            get => _selectedStudent;
            set => SetProperty(ref _selectedStudent, value); // 使用 SetProperty 方法简化逻辑
        }

        private PagedResultViewModel _pagedResultViewModel = new PagedResultViewModel();

        public PagedResultViewModel PagedResultViewModel
        {
            get => _pagedResultViewModel;
            set => SetProperty(ref _pagedResultViewModel, value);
        }



        private User1viewModel _user1viewModel = new User1viewModel();

        public User1viewModel User1viewModel
        {
            get => _user1viewModel;
            set => SetProperty(ref _user1viewModel, value);
        }

        #endregion

        #region 命令
        //查询命令
        public RelayCommand QueryCommand { get; set; }

        //查找命令
        public RelayCommand<string> InputCommand { get; set; }

        //添加命令
        public RelayCommand AddCommand { get; set; }

        //修改命令
        public RelayCommand<int> EditCommand { get; set; }

        public RelayCommand Edit1Command { get; set; }

        //删除命令
        public RelayCommand<int> DelCommand { get; set; }

        #endregion

        #region 无参构造函数
        public MainWindowViewModel()
        {
            //查询数据的方法
            QueryCommand = new RelayCommand(Query);
            InputCommand = new RelayCommand<string>(Input);
            AddCommand = new RelayCommand(Add);
            EditCommand = new RelayCommand<int>(Edit);

            Edit1Command = new RelayCommand(Edit1);

            DelCommand = new RelayCommand<int>(Delete);

            PagedResultViewModel.FirstPage();

            PagedResultViewModel.Alert = x =>
            {
                //CityIsEnable = x;
                //OperatorIsEnable = x;
            };
            //Query();
        }
        #endregion



        public void Query()
        {
            //先清除数据
            //Students.Clear();
            Students = SqlHelper.ExecuteReader();
        }

     

        public void Input(string parameter)
        {
            var parameter1 = parameter.Trim();
            if (string.IsNullOrEmpty(parameter1))
            {
                var filter = "1=1";
                PagedResultViewModel.Filter = filter;
                PagedResultViewModel.FirstPage();
            }
            else
            {
                //之前使用的，不能够进行模糊搜索，淘汰
                //var filter = $"Name = '{parameter1}'";

                //新的语句，可以使用模糊搜索
                var filter = $"Name LIKE '%{parameter1}%'";
                PagedResultViewModel.Filter = filter ;
                PagedResultViewModel.FirstPage();
            }
        }

        public void Add()
        {
            // 创建对话框窗口实例
            var dialog = new User1();
            dialog.Owner = Application.Current.MainWindow; // 设置拥有者
            dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            

            // 显示模态对话框
            bool? result = dialog.ShowDialog();
            if (result == true)
            {
                Students userInput = dialog.Students;
                SqlHelper.WriteOperate(userInput);
                MessageBox.Show("添加成功，数据已经存在最后一行！！！");
            }
            else
            {
                MessageBox.Show("添加失败，请检查方法！！！");
            }
        }


        public void Edit(int parameter)
        {
            if (parameter != default(int))
            {
                var filter = $"Id = '{parameter}'";
                List<Students> students =  SqlHelper.ExecuteReader(filter);

                // 创建对话框窗口实例
                var dialog = new User1();
                dialog.txtName.Text = students[0].Name;
                dialog.txtPassWord.Text = students[0].PassWord;
                dialog.Owner = Application.Current.MainWindow; // 设置拥有者
                dialog.WindowStartupLocation = WindowStartupLocation.CenterOwner;

                //显示模态对话框
                bool? result = dialog.ShowDialog();
                if (result == true)
                {
                    Students userInput = new Students
                    {
                        Id = parameter,
                        Name = dialog.Students.Name,
                        PassWord = dialog.Students.PassWord
                    };
                    SqlHelper.UpdateOperate(userInput);

                    //刷新页面
                    PagedResultViewModel.RefreshPage();
                }
                else
                {

                }
            }
        }


        public void Edit1()
        {
            var AAA = 123;
        }


        public void Delete(int paramater)
        {
            MessageBoxResult confirmToDel = MessageBox.Show("确认要删除所选行吗？", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (confirmToDel == MessageBoxResult.Yes)
            {
                var filter = $"Id = '{paramater}'";
                SqlHelper.DeleteOperate(filter);

                //刷新页面
                PagedResultViewModel.RefreshPage();
            }        
        }
    }
}

